<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>个人信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-descriptions class="margin-top" title="个人信息" :column="3" border>
        <template slot="extra">
          <el-button type="primary" size="small" @click="setMessage">完善信息</el-button>
        </template>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            用户名
          </template>
          {{ message.username }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-message"></i>
            邮箱
          </template>
          {{ message.email }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            电话
          </template>
          {{ message.phoneNumber }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-s-custom"></i>
            姓名
          </template>
          {{ message.name }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            性别
          </template>
          {{ message.sex }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            国籍
          </template>
          {{ message.nationality }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            地址
          </template>
          {{ message.address }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-date"></i>
            出生日期
          </template>
          {{ message.dateOfBirth }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-postcard"></i>
            身份证号
          </template>
          {{ message.identityCard }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            科目一剩余课时
          </template>
          {{ message.subjectOneTime }} 小时
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            科目二剩余课时
          </template>
          {{ message.subjectTwoTime }} 小时
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            科目三剩余课时
          </template>
          {{ message.subjectThreeTime }} 小时
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            科目四剩余课时
          </template>
          {{ message.subjectFourTime }} 小时
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-notebook-2"></i>
            科目一剩余考试次数
          </template>
          {{ message.subjectOneExam }} 次
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-notebook-2"></i>
            科目二剩余考试次数
          </template>
          {{ message.subjectTwoExam }} 次
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-notebook-2"></i>
            科目三剩余考试次数
          </template>
          {{ message.subjectThreeExam }} 次
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-notebook-2"></i>
            科目四剩余考试次数
          </template>
          {{ message.subjectFourExam }} 次
        </el-descriptions-item>
      </el-descriptions>
    </el-card>

    <!-- 完善信息的对话框 -->
    <el-dialog title="完善信息" :visible.sync="setMessageDialogVisible" width="50%">
      <!-- 内容主体区域 -->
      <el-form :model="newMessage" :rules="messageRules" ref="addUserFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="newMessage.username" placeholder="请输入用户名" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="newMessage.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phoneNumber">
          <el-input v-model="newMessage.phoneNumber" placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="newMessage.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio v-model="newMessage.sex" v-for="item in sexOptions" :key="item"
                    :label="item">{{ item }}
          </el-radio>
        </el-form-item>
        <el-form-item label="国籍" prop="nationality">
          <el-input v-model="newMessage.nationality" disabled></el-input>
        </el-form-item>
        <el-form-item label="住址" prop="address">
          <el-input v-model="newMessage.address" placeholder="请输入住址"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="dateOfBirth">
          <el-date-picker v-model="newMessage.dateOfBirth" type="date" value-format="yyyy-MM-dd"
                          placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="身份证" prop="identityCard">
          <el-input v-model="newMessage.identityCard" placeholder="请输入身份证号"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelSetMessage">取 消</el-button>
        <el-button type="primary" @click="completeMessage">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "StudentMessage",
  data(){
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/

      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }

      cb(new Error('请输入合法的邮箱'))
    }

    // 验证手机号的规则
    var checkPhoneNumber = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regPhoneNumber = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regPhoneNumber.test(value)) {
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }

    // 校验身份证号
    var checkIdentityCard = (rule, value, cb) => {
        // 验证身份证号的正则表达式
        const regIdentityCard = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/

        if (regIdentityCard.test(value)) {
          return cb()
        }

        cb(new Error('请输入合法的身份证号'))
      }
    return{
      // 学员信息
      message: {},
      newMessage: {},
      setMessageDialogVisible: false,
      sexOptions: ['男', '女'],
      messageRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {
            min: 3,
            max: 16,
            message: '用户名的长度在3~16个字符之间',
            trigger: 'blur'
          }
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {validator: checkEmail, trigger: 'blur'}
        ],
        phoneNumber: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {validator: checkPhoneNumber, trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {
            min: 2,
            max: 14,
            message: '用户名的长度在2~14个字符之间',
            trigger: 'blur'
          }
        ],
        sex:[
          {required: true, message: '请选择性别', trigger: 'blur'},
        ],
        address: [
          {required: true, message: '请输入地址', trigger: 'blur'},
        ],
        dateOfBirth: [
          {required: true, message: '请输入出生日期', trigger: 'blur'},
        ],
        identityCard: [
          {required: true, message: '请输入身份证号', trigger: 'blur'},
          {validator: checkIdentityCard, trigger: 'blur'}
        ]
      },
    }
  },
  created() {
    this.getMessage()
  },
  methods:{
    // 获取学员个人信息
    getMessage(){
      this.$http.get('/driver/student/getMessage').then(res => {
        if (res.data.code === 200) {
          this.message = res.data.data
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    },
    setMessage(){
      this.newMessage = {...this.message}
      this.setMessageDialogVisible = true
    },
    cancelSetMessage(){
      this.setMessageDialogVisible = false
    },
    // 完善学员信息
    completeMessage(){
      this.$http.post('/driver/student/completeMessage', this.newMessage).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          this.setMessageDialogVisible = false
          this.getMessage()
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
